home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / SAT 2.3b4 / Tutorial ƒ / Assignment6.p < prev    next >
Text File  |  1995-05-01  |  4KB  |  157 lines

  1. program Assignment6;
  2.     uses
  3. {$ifc UNDEFINED THINK_PASCAL}
  4.         Types, QuickDraw, Menus, Windows, TextEdit, Fonts, Dialogs, Memory, OSEvents, {}
  5. {$endc}
  6.         SAT;
  7.  
  8.     const
  9.         kSpeed = 5;
  10.     var
  11.         ignore: SpritePtr;
  12.         direction: Integer;
  13.         theSound: Handle;
  14.         score: Longint;
  15.         startTime: Longint;
  16.         doneFlag: Boolean;
  17.  
  18.     procedure HandleSprite (me: SpritePtr);
  19.         var
  20.             event: EventRecord;
  21.     begin
  22. {Now hold on to the hat! I'm using GetOSEvent to get key down events.}
  23. {the keydowns then affect the speed variable in the sprite. That speed}
  24. {is added to the position. Finally, I check the position against the screen}
  25. {borders, and modify the speed in case I reach a border.}
  26.  
  27.         if GetOSEvent(keyDownMask, event) then
  28.             if event.what = keyDown then
  29.                 case char(BAnd(event.message, charCodeMask)) of
  30.                     'a': 
  31.                         me^.speed.h := me^.speed.h - 1;
  32.                     's': 
  33.                         me^.speed.h := me^.speed.h + 1;
  34.                     'w': 
  35.                         me^.speed.v := me^.speed.v - 1;
  36.                     'z': 
  37.                         me^.speed.v := me^.speed.v + 1;
  38.                 end;
  39.  
  40.         me^.position.h := me^.position.h + me^.speed.h;
  41.         me^.position.v := me^.position.v + me^.speed.v;
  42.         if me^.position.h < 0 then
  43.             begin
  44.                 me^.speed.h := abs(me^.speed.h);
  45.                 me^.position.h := 0;
  46.             end;
  47.         if me^.position.h > gSAT.offSizeH - 32 then
  48.             begin
  49.                 me^.speed.h := -abs(me^.speed.h);
  50.                 me^.position.h := gSAT.offSizeH - 32;
  51.             end;
  52.         if me^.position.v < 0 then
  53.             begin
  54.                 me^.speed.v := abs(me^.speed.v);
  55.                 me^.position.v := 0;
  56.             end;
  57.         if me^.position.v > gSAT.offSizeV - 32 then
  58.             begin
  59.                 me^.speed.v := -abs(me^.speed.v);
  60.                 me^.position.v := gSAT.offSizeV - 32;
  61.             end;
  62.     end; {HandleSprite}
  63.  
  64.     procedure SetupSprite (me: SpritePtr);
  65.     begin
  66.         me^.task := @HandleSprite;
  67.         me^.face := SATGetFace(128);
  68.         me^.speed := Point(0);
  69.         SetRect(me^.hotRect, 0, 0, 32, 32);
  70.     end; {SetupSprite}
  71.  
  72.     procedure SetupTarget (me: SpritePtr);
  73.     forward;
  74.  
  75.     procedure HandleTarget (me: SpritePtr);
  76.     begin
  77. {The target sprite isn't modified much. I just limit movement after gSAT.offSizeH instead}
  78. {of a hard-coded constant.}
  79.         me^.position.h := me^.position.h + direction;
  80.         if me^.position.h <= 0 then
  81.             direction := kSpeed;
  82.         if me^.position.h >= gSAT.offSizeH - 32 then
  83.             direction := -kSpeed;
  84.     end; {HandleTarget}
  85.  
  86.     procedure HitTarget (me, him: SpritePtr);
  87.         var
  88.             savePort: SATPort;
  89.             r: Rect;
  90.     begin
  91.         if him^.task = @HandleSprite then {Chack what we hit!}
  92.             begin
  93.                 me^.task := nil;
  94.                 ignore := SATNewSprite(-1, 0, SATRand(gSAT.offSizeV - 32), @SetupTarget);
  95. {We could also re-use the old sprite for a new one, if we like.}
  96.                 SATSoundPlay(theSound, 1, true);
  97.  
  98. {Add to the score}
  99.                 score := score + 1;
  100.  
  101. {Draw the score on the screen.}
  102.                 SATGetPort(savePort); {Save port and device!}
  103.                 SATSetPortBackScreen;
  104.                 SetRect(r, 100, 0, 100 + stringWidth(stringof('Caught: ', score : 1)), 15);
  105.                 EraseRect(r);
  106.                 MoveTo(r.left, r.bottom - 3);
  107.                 DrawString(stringof('Caught: ', score : 1));
  108.                 SATBackChanged(r);
  109.                 SATSetPort(savePort); {Always restore!}
  110.  
  111.                 if score = 10 then
  112.                     doneFlag := true;
  113.             end;
  114.     end; {HitTarget}
  115.  
  116.     procedure SetupTarget (me: SpritePtr);
  117.     begin
  118.         me^.task := @HandleTarget;
  119.         me^.hitTask := @HitTarget;
  120.         me^.face := SATGetFace(129);
  121.         SetRect(me^.hotRect, 0, 0, 32, 32);
  122.         direction := kSpeed;
  123.     end; {SetupTarget}
  124.  
  125.     const
  126.         kTicksPerFrame = 2;
  127.     var
  128.         t: Longint;
  129.  
  130. begin
  131. {If we don't use Think Pascal, we must make standard inits ourselves.}
  132. {$ifc UNDEFINED THINK_PASCAL}
  133.     SATInitToolbox;
  134. {$endc}
  135.  
  136.     SATConfigure(false, kVPositionSort, kForwardCollision, 32);
  137.     SATInit(128, 129, 478, 302);
  138.     ignore := SATNewSprite(0, 200, 200, @SetupSprite);
  139.     ignore := SATNewSprite(0, 0, SATRand(gSAT.offSizeV), @SetupTarget);
  140.     theSound := SATGetNamedSound('TestSound');
  141.     score := 0;
  142.     HideCursor;
  143.     startTime := TickCount;
  144.     doneFlag := false;
  145.     while not Button and not doneFlag do
  146.         begin
  147.             t := TickCount;
  148.             SATRun(true);
  149.             while TickCount < t + kTicksPerFrame do
  150.                 ;
  151.         end;
  152.     ShowCursor;
  153. {Extremely simple result report. A real game should, of course, display this in the game}
  154. {window, in a prettier way, perhaps with a high score list, etc.}
  155.     SATReportStr(stringof('Time to catch ', score : 1, ' disks: ', (TickCount - startTime) div 60 : 1, ' seconds.'));
  156.     SATSoundShutup;
  157. end.